﻿using System;
using System.Threading;

namespace InfiniTec.Threading
{
	public sealed class AsyncResultWrapper: IDisposable
	{
		private readonly IAsyncResult _AsyncResult;
		private WaitHandle _Handle;

		public AsyncResultWrapper(IAsyncResult asyncResult)
		{
			if (asyncResult == null) throw new ArgumentNullException("asyncResult");
			_AsyncResult = asyncResult;
		}

		public WaitHandle AsyncWaitHandle
		{
			get
			{
				if (_Handle == null)
				{
					_Handle = _AsyncResult.AsyncWaitHandle;
				}
				return _AsyncResult.AsyncWaitHandle;
			}
		}

		public IAsyncResult AsyncResult
		{
			get
			{
				return _AsyncResult;
			}
		}

		public void Dispose()
		{
			if (_Handle != null)
			{
				_Handle.Close();
			}
		}
	}
}
